package cn.sunzq.springdata.elasticsearch.demo.config;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Configuration;

/**
 * @创建人 孙中泉
 * @创建时间 2019/11/27
 * @描述
 */
@Configuration
public class HikariBaseConfig {

    @Value("${spring.datasource.hikari.pool-name:KevinHikariPool}")
    private String poolName;
    @Value("${spring.datasource.hikari.maximum-pool-size:150}")
    private Integer maximumPoolSize;
    @Value("${spring.datasource.hikari.connection-timeout:60000}")
    private Long connectionTimeout;
    @Value("${spring.datasource.hikari.minimum-idle:30}")
    private Integer minimumIdle;
    @Value("${spring.datasource.hikari.idle-timeout:500000}")
    private Long idleTimeout;
    @Value("${spring.datasource.hikari.max-lifetime:600000}")
    private Long maxLifetime;
    @Value("${spring.datasource.hikari.connection-test-query:SELECT 1}")
    private String connectionTestQuery;


    public HikariDataSource getDataSource(String driverClassName, String url, String username, String password){
        HikariDataSource hikariDataSource = DataSourceBuilder.create().type(HikariDataSource.class).driverClassName(driverClassName).username(username).url(url).password(password).build();
        hikariDataSource.setConnectionTestQuery(connectionTestQuery);
        hikariDataSource.setMaxLifetime(maxLifetime);
        hikariDataSource.setMinimumIdle(minimumIdle);
        hikariDataSource.setIdleTimeout(idleTimeout);
        hikariDataSource.setConnectionTimeout(connectionTimeout);
        hikariDataSource.setPoolName(poolName);
        hikariDataSource.setMaximumPoolSize(maximumPoolSize);
        return hikariDataSource;
    }


}
